package com.tuniu.agents.tools.widgets.tool;

import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.tuniu.agents.message.ToolLoggerInfo;
import com.tuniu.agents.tools.widgets.WidgetsProperties;
import com.tuniu.agents.util.HttpManager;
import com.tuniu.agents.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.function.BiFunction;

public class RestaurantListTool implements BiFunction<RestaurantListTool.Request, ToolContext, RestaurantListTool.ReqRespWrap> {
    private static final Logger log = LoggerFactory.getLogger(RestaurantListTool.class);

    public static final String TOOLNAME = "restaurant_list";

    @Autowired
    private HttpManager httpManager;

    private final String url;

    public RestaurantListTool(WidgetsProperties properties) {
        this.url = properties.getQueryRestaurantUrl();
    }

    @ToolLoggerInfo(beanName = "restaurantToolLoggingService")
    public ReqRespWrap apply(Request request, ToolContext toolContext) {

        log.info("RestaurantListTool#apply param:{}", JsonUtil.toJson(request));
        try {
            Response response = httpManager.get(url, request, Response.class);
            log.info("RestaurantListTool#apply response:{}", JsonUtil.toJson(response));
            return new ReqRespWrap(request, response);
        } catch (Exception e) {
            log.error("RestaurantListTool#apply error request:{}", JsonUtil.toJson(request), e);
        }
        return null;
    }

    @JsonClassDescription("restaurant request response wrap")
    public record ReqRespWrap(
          @JsonProperty(value = "request") Request request,
          @JsonProperty(value = "response") Response response
    ){}

    @JsonClassDescription("restaurant request")
    public record Request(
            @JsonProperty(required = true, value = "placeName") @JsonPropertyDescription("place name") String placeName,
            @JsonProperty(defaultValue = "", value = "location") @JsonPropertyDescription("Poi longitude and latitude separated by commas") String location,
            @JsonProperty(defaultValue = "1", value = "pageNum") @JsonPropertyDescription("page number") Integer pageNum,
            @JsonProperty(value = "platCode") @JsonPropertyDescription("platform code 1 Gaode 2 Baidu default Gaode") Integer platCode
    ){}

    @JsonClassDescription("restaurant response")
    public record Response(
            @JsonProperty(value = "success") @JsonPropertyDescription("is it successful") boolean success,
            @JsonProperty(value = "errorCode") @JsonPropertyDescription("error code") int errorCode,
            @JsonProperty(value = "msg") @JsonPropertyDescription("error message") String msg,
            @JsonProperty(value = "data") @JsonPropertyDescription("restaurant information list") List<RestaurantListTool.Restaurant> data
    ){}

    @JsonClassDescription("restaurant")
    public record Restaurant(
            @JsonProperty(value = "name") @JsonPropertyDescription("restaurant name") String name,
            @JsonProperty(value = "location") @JsonPropertyDescription("Poi longitude and latitude separated by commas") String location,
            @JsonProperty(value = "type") @JsonPropertyDescription("restaurant type") String type,
            @JsonProperty(value = "typecode") @JsonPropertyDescription("restaurant type code") String typecode,
            @JsonProperty(value = "pname") @JsonPropertyDescription("province name") String pname,
            @JsonProperty(value = "cityname") @JsonPropertyDescription("city name") String cityname,
            @JsonProperty(value = "adname") @JsonPropertyDescription("district") String adname,
            @JsonProperty(value = "address") @JsonPropertyDescription("address") String address,
            @JsonProperty(value = "pcode") @JsonPropertyDescription("province code") String pcode,
            @JsonProperty(value = "citycode") @JsonPropertyDescription("city code") String citycode,
            @JsonProperty(value = "adcode") @JsonPropertyDescription("district code") String adcode,
            @JsonProperty(value = "distance") @JsonPropertyDescription("distance") String distance,
            @JsonProperty(value = "telephone") @JsonPropertyDescription("telephone") String telephone,
            @JsonProperty(value = "rating") @JsonPropertyDescription("rating") String rating,
            @JsonProperty(value = "opentimeToday") @JsonPropertyDescription("opentime today") String opentimeToday,
            @JsonProperty(value = "opentimeWeek") @JsonPropertyDescription("opentime week") String opentimeWeek,
            @JsonProperty(value = "tag") @JsonPropertyDescription("featured content") String tag,
            @JsonProperty(value = "businessArea") @JsonPropertyDescription("business area") String businessArea
    ){}

}
